#include "FunctionDef.h"
#include "IVisitor.h"

FunctionDef::FunctionDef(const NameTokPtr& name, const argumentsTypePtr& args, const StmtTypeList& body, const DecoratorsTypeList& decs, const exprTypePtr& returns, int nodeId):
    stmtType(nodeId),
    _name(name),
    _args(args),
    _body(body),
    _decs(decs),
    _returns(returns)
{
}
    
FunctionDef::~FunctionDef()
{
    _body.clear();_decs.clear();
}
    
QString FunctionDef::type() const
{
    return "FunctionDef";
}
    

NameTokPtr FunctionDef::name()
{
    return _name;
}

void FunctionDef::setName(const NameTokPtr& name)
{
    _name = name;
}

argumentsTypePtr FunctionDef::args()
{
    return _args;
}

void FunctionDef::setArgs(const argumentsTypePtr& args)
{
    _args = args;
}

StmtTypeList FunctionDef::body()
{
    return _body;
}

void FunctionDef::setBody(const StmtTypeList& body)
{
    _body = body;
}

DecoratorsTypeList FunctionDef::decs()
{
    return _decs;
}

void FunctionDef::setDecs(const DecoratorsTypeList& decs)
{
    _decs = decs;
}

exprTypePtr FunctionDef::returns()
{
    return _returns;
}

void FunctionDef::setReturns(const exprTypePtr& returns)
{
    _returns = returns;
}

    
QString FunctionDef::toString() const
{
    return QString("FunctionDef[name=%1, args=%2, body=%3, decs=%4, returns=%5]").
                arg(_name ? _name->toString():"").
                arg(_args ? _args->toString():"").
                arg(_body.toString()).
                arg(_decs.toString()).
                arg(_returns ? _returns->toString():"");
}
    
void FunctionDef::accept(IVisitor* visitor, const SimpleNodePtr& node)
{
    visitor->visitFunctionDef(node.dynamicCast<FunctionDef>());
}
    
void FunctionDef::traverse(IVisitor* visitor)
{
    if (_name != NULL)
        _name->accept(visitor, _name);
    if (_args != NULL)
        _args->accept(visitor, _args);
    _body.traverse(visitor);
    _decs.traverse(visitor);
    if (_returns != NULL)
        _returns->accept(visitor, _returns);
}